package com.qingshu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingshu.common.entity.sys.SysUser;
import com.qingshu.common.entity.sys.vo.UserVO;
import com.qingshu.common.util.model.FieldUtil;
import com.qingshu.common.util.model.ModelUtil;
import com.qingshu.common.util.security.SecurityUtils;
import com.qingshu.common.util.string.StringUtils;
import com.qingshu.mapper.UserMapper;
import com.qingshu.service.IUserRoleService;
import com.qingshu.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Huzz
 * @since 2021-10-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, SysUser> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private ModelUtil modelUtil;

    @Override
    public boolean register(SysUser sysUser) {
        if (StringUtils.isNull(sysUser)
                || StringUtils.isEmpty(sysUser.getUsername())
                || StringUtils.isEmpty(sysUser.getPassword())) {
            return false;
        }
        FieldUtil.setCreatedAndUpdateField(sysUser);

        return userMapper.insert(sysUser) > 0;
    }

    @Override
    public boolean updateUser(SysUser sysUser) {
        if (sysUser.getId() == null || "".equals(sysUser.getId())) {
            return false;
        }
        FieldUtil.setUpdateField(sysUser);
        return userMapper.updateById(sysUser) > 0;
    }

    @Override
    public boolean delUser(Integer userId) {
        // 清除该用户对应的角色信息
        userRoleService.removeByUserId(userId);
        return userMapper.deleteById(userId) > 0;
    }

    @Override
    public SysUser findUserByUsername(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysUser::getUsername, username);

        return userMapper.selectOne(wrapper);
    }

    @Override
    public Page<UserVO> findUserPage(Integer pageNum, Integer pageSize, UserVO model) {
        // 查询条件
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(model.getSex())) {
            wrapper.eq(SysUser::getSex, model.getSex());
        }
        if (StringUtils.isNotEmpty(model.getUsername())) {
            wrapper.eq(SysUser::getUsername, model.getUsername());
        }
        if (model.getBirth() != null) {
            wrapper.eq(SysUser::getBirth, model.getBirth());
        }
        if (StringUtils.isNotEmpty(model.getNickname())) {
            wrapper.eq(SysUser::getNickname, model.getNickname());
        }
        if (model.getUserType() != null) {
            wrapper.eq(SysUser::getUserType, model.getUserType());
        }
        if (model.getStatus() != null) {
            wrapper.eq(SysUser::getStatus, model.getStatus());
        }
        if (model.getCreatedUserId() != null) {
            wrapper.eq(SysUser::getCreatedUserId, model.getCreatedUserId());
        }
        // 分页查询
        Page<SysUser> page = new Page<>(pageNum, pageSize);
        Page<SysUser> userPage = userMapper.selectPage(page, wrapper);

        // 转换为VO类型的page
        return modelUtil.toPageEntity(userPage, UserVO.class);
    }

    @Override
    public SysUser findUserById(Integer userId) {

        return this.userMapper.selectById(userId);
    }

}
